﻿using LY.Common.Utils.Sf_Struct;
using System;
using System.Net;
using System.Text;

namespace LY.ShengPayApi.Services
{
    public class Post
    {
        /// <summary>
        /// 用户投标（自动）
        /// </summary>
        /// <param name="merchantNo">平台商户号（由盛付通分配，请联系盛付通商务获取）</param>
        /// <param name="issueBidNo">平台发标编号（由平台生成，并在平台中保证唯一）</param>
        /// <param name="submitBidNo">平台投标编号（由平台生成，并在平台中保证唯一）</param>
        /// <param name="creditUserName">平台用户账户（由平台生成，并在平台中保证唯一）</param>
        /// <param name="creditAmount">平台用户投资金额（用户实际支付金额：creditAmount - discountAmount）（以元为单位，仅允许两位小数）</param>
        /// <param name="discountAmount">平台给予用户的折扣金额（以元为单位，仅允许两位小数。譬如：600.00）</param>
        /// <param name="notifyUrl">异步通知回调地址</param>
        /// <param name="apiAddress">盛付通API接口</param>
        /// <returns></returns>
        public static string ZiDongTouBiao(string merchantNo,string issueBidNo,string submitBidNo,string creditUserName,string creditAmount,string discountAmount,string notifyUrl,string apiAddress)
        {
            string[] mysign = new string[7];
            mysign[0] = "merchantNo=" + merchantNo;
            mysign[1] = "issueBidNo=" + issueBidNo;
            mysign[2] = "submitBidNo=" + submitBidNo;
            mysign[3] = "creditUserName=" + creditUserName;
            mysign[4] = "creditAmount=" + creditAmount;
            mysign[5] = "discountAmount=" + discountAmount;
            mysign[6] = "notifyUrl=" + notifyUrl; 
            Array array = mysign;
            Array.Sort(array);
            string sign = string.Join("&", mysign);
            string temp = new MySign().GetSign(sign);
            string newsign = WebUtility.HtmlDecode(temp);
            WebClient wc = new WebClient();
            string postString = sign + "&sign=" + newsign;
            byte[] sendData = Encoding.UTF8.GetBytes(postString);
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            wc.Headers.Add("ContentLength", sendData.Length.ToString());
            string url = apiAddress + "/p2pBid/" + merchantNo + "-" + issueBidNo + "/submitBid";
            byte[] recData = wc.UploadData(url, "POST", sendData);
            return Encoding.UTF8.GetString(recData);
        }

        /// <summary>
        /// 生成债券接口
        /// </summary>
        /// <param name="merchantNo">平台商户号（由盛付通分配，请联系盛付通商务获取）</param>
        /// <param name="tradableBondNo">生成债券订单号</param>
        /// <param name="parValue">生成债券面额（以元为单位，仅允许两位小数。譬如：600.00）</param>
        /// <param name="debtorType">债券方类型（1：个人 2：企业）</param>
        /// <param name="debtorUserName">平台用户账户（当debtorType=1时，该字段必填）（由平台生成，并在平台中保证唯一）</param>
        /// <param name="debtorInstMerchantNo">融资机构商户号（当debtorType=2时，该字段必填）（由盛付通分配，请联系盛付通商务获取）</param>
        /// <param name="creditorType">债权人类型（1：个人 2：企业）</param>
        /// <param name="creditorUserName">平台用户账户（当creditorType=1时，该字段必填）（由平台生成，并在平台中保证唯一）</param>
        /// <param name="creditorInstMerchantNo">融资机构商户号（当creditorType=2时，该字段必填）（由盛付通分配，请联系盛付通商务获取）</param>
        /// <param name="revenueRate">年化收益率（以百分比为单位，仅允许两位小数。譬如：20.79）</param>
        /// <param name="deadline">借款期限（以天为单位，仅允许自然数，例如365）</param>
        /// <param name="repaymentMode">还款方式</param>
        /// <param name="apiAddress">盛付通API接口</param>
        /// <returns></returns>
        public static string Bond(string merchantNo, string tradableBondNo, string parValue, string debtorType, string debtorUserName, string debtorInstMerchantNo
            , string creditorType, string creditorUserName, string creditorInstMerchantNo, string revenueRate, string deadline, string repaymentMode,string apiAddress )
        {
            string[] mysign = new string[10];
            mysign[0] = "merchantNo=" + merchantNo;
            mysign[1] = "timestamp=" + DateTime.Now.ToString("yyyyMMddHHmmss");
            mysign[2] = "tradableBondNo=" + tradableBondNo;
            mysign[3] = "parValue=" + parValue;
            mysign[4] = "debtorType=" + debtorType;
            if (debtorType == "1") //个人
            {
                mysign[5] = "debtorUserName=" + debtorUserName;
            }
            else //商户
            {
                mysign[5] = "debtorInstMerchantNo=" + debtorInstMerchantNo;
            }
            mysign[6] = "creditorType=" + creditorType;
            if (creditorType == "1") //个人
            {
                mysign[7] = "creditorUserName=" + creditorUserName;
            }
            else //商户
            {
                mysign[7] = "creditorInstMerchantNo=" + creditorInstMerchantNo;
            }
            mysign[8] = "revenueRate=" + revenueRate;
            mysign[9] = "deadline=" + deadline;
            mysign[9] = "repaymentMode=" + repaymentMode;
            Array array = mysign;
            Array.Sort(array);
            string sign = string.Join("&", mysign);
            string temp = new MySign().GetSign(sign);
            string newsign = WebUtility.HtmlDecode(temp);
            WebClient wc = new WebClient();
            string postString = sign + "&sign=" + newsign;
            byte[] sendData = Encoding.UTF8.GetBytes(postString);
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            wc.Headers.Add("ContentLength", sendData.Length.ToString());
            string url = apiAddress + "/tradableBond/" + merchantNo + "-" + tradableBondNo; //tradableBond/{merchantNo}-{tradableBondNo}
            byte[] recData = wc.UploadData(url, "POST", sendData);
            return Encoding.UTF8.GetString(recData);
        }

        public static void PostP2PBidSubmitBid(string serverUrl, string merchantNo, string issueBidNo)
        {
            //p2pBid/{merchantNo}-{issueBidNo}/submitBid
        }
     
    }
}
